package com.suedawn.bookmanagesystem.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suedawn.bookmanagesystem.entity.Book;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "分页查询实体")
public class PageQueryDTO {
    @ApiModelProperty("页码")
    private Integer pageNo = 1;
    @ApiModelProperty("页码长度")
    private Integer pageSize = 10;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPageWithoutSort() {
        return Page.of(pageNo, pageSize);
    }

    /**
     * 转换为MyBatis-Plus分页对象
     * @param needSort 是否需要排序
     */
    public <T> Page<T> toMpPage(boolean needSort, OrderItem... items) {
        Page<T> page = Page.of(pageNo, pageSize);
        if (needSort && StrUtil.isNotBlank(sortBy)) {
            page.addOrder(new OrderItem(sortBy, isAsc));
        } else if (needSort && items != null) {
            page.addOrder(items);
        }
        return page;
    }

    public <T> Page<T> toMpPage(OrderItem... items){
        // 1.分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        // 2.排序条件
        if(StrUtil.isNotBlank(sortBy)){
            // 不为空
            page.addOrder(new OrderItem(sortBy, isAsc));
        }else if(items != null){
            // 为空，默认排序
            page.addOrder(items);
        }
        return page;
    }
    /**
     * 检查分页结果是否为空
     */
//    public static <T> boolean isEmpty(com.github.pagehelper.Page<Book> page) {
//        return page == null || page.getTotal() <= 0 || CollUtil.isEmpty(page.getRecords());
//    }

}
